package com.sys.controller;

import com.sys.common.JsonResult;
import com.sys.common.O_Search;
import com.sys.common.P_Search;
import com.sys.common.Pid;
import com.sys.dto.OrderDTO;
import com.sys.dto.ProductDTO;
import com.sys.dto.UserDTO;
import com.sys.dto.idto.IOrderDTO;
import com.sys.entity.OrdersEntity;
import com.sys.entity.ProductEntity;
import com.sys.entity.UserEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@RequestMapping("/products")
public class ProductController extends BaseController{

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult<List<ProductEntity>> findAll(){
        ProductDTO productDTO = new ProductDTO();
        List<ProductEntity> all = productDTO.findAll();
        JsonResult<List<ProductEntity>> listJsonResult = new JsonResult<>(EOK, all);
        return new JsonResult<List<ProductEntity>>(EOK, all);
    }
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Void> reg(ProductEntity entity){
        ProductDTO productDTO = new ProductDTO();
        productDTO.save(entity);
        return new JsonResult<>(EOK);
    }
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Void> del(Integer pid,HttpSession session){
        ProductDTO productDTO = new ProductDTO();
        productDTO.delete(pid);
        return new JsonResult<>(EOK);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Void> update(ProductEntity entity){
        Integer pid = entity.getPid();
        ProductDTO productDTO = new ProductDTO();
        productDTO.update(entity,pid);

        System.out.println("修改后的商品是："+entity.getProduceName());
        return new JsonResult<>(EOK);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public JsonResult<Pid> getPid(Integer pid){
        return new JsonResult<Pid>(EOK);
    }

    @RequestMapping(value = "/findsearch", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<List<ProductEntity>> findsearch(P_Search p_search){
        System.out.println("搜索条件是："+p_search);
        String pid =  p_search.getPid();
        String produceName = p_search.getProduceName();
        String type = p_search.getType();
        if (pid != null && !pid.equals("")){
            System.out.println("根据订单号查询");
            System.out.println("订单号是："+pid);
            ProductDTO productDTO = new ProductDTO();
            List<ProductEntity> byProperty = productDTO.findByProperty("pid", pid);
            return new JsonResult<List<ProductEntity>>(EOK, byProperty);
        } else if (produceName != null && !produceName.equals("")) {
            System.out.println("根据商品名查询");
            System.out.println("商品名是："+produceName);
            ProductDTO productDTO = new ProductDTO();
            List<ProductEntity> byProperty = productDTO.findByProperty("produceName", produceName);
            return new JsonResult<List<ProductEntity>>(EOK, byProperty);
        } else if (type != null && !type.equals("")) {
            System.out.println("根据客户名查询");
            System.out.println("客户名是："+type);
            ProductDTO productDTO = new ProductDTO();
            List<ProductEntity> byProperty = productDTO.findByProperty("type", type);
            return new JsonResult<List<ProductEntity>>(EOK, byProperty);
        }else
            return new JsonResult<List<ProductEntity>>(EOK);
    }
}
